package algorithm.lanqiao.t110;

import algorithm.util.IOUtil;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

public class Main {
    static class IO {
        // 读
        static InputStreamReader ins = new InputStreamReader(System.in);
        static StreamTokenizer in = new StreamTokenizer(ins);
        static BufferedReader br = new BufferedReader(ins);

        // 写
        public static PrintWriter out = new PrintWriter(System.out);


        public static int readInt() {
            try {
                in.nextToken();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }

        public static String readLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static int N = 1000010;
    static int[] fa = new int[N];
    static int find(int x) {
        if(fa[x] == x) return x;
        return fa[x] = find(fa[x]);
    }
    public static void main(String[] args) {
        int n = IO.readInt(), m = IO.readInt();
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                int num = (i-1) * m + j;
                fa[num] = num;
            }
        }
        int k = IO.readInt();
        for(int i = 1; i <= k; i ++) {
            int a = IO.readInt(), b = IO.readInt();
            fa[find(b)] = find(a);
        }
        Set<Integer> set = new HashSet<>();
        for(int i = 1; i <= m; i ++) {
            for(int j = 1; j <= n; j ++) {
                int num = (i-1) * m + j;
                set.add(find(num));
            }
        }
        IO.out.println(set.size());
        IO.out.flush();

    }
}
